const pageHelper = require('../../../../../helper/page_helper.js');
const timeHelper = require('../../../../../helper/time_helper.js')
const ProjectBiz = require('../../../biz/project_biz.js');

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: true,
		mainCur: 0,
		verticalNavTop: 0,

		dayIdx: 0,
		timeIdx: -1,

		dateList:[],
		dateTime: ['09:00-10:00','10:00-11:00']
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		ProjectBiz.initPage(this);

		const dateTime = ['09:00-10:00', '10:00-11:00']
		const days = this.getFutureDate(7).map(v=>{
			return {
				day:v,
				status:"",
				times:dateTime.map((m,i) =>{
					return {
						start: m.split('-')[0],
						end: m.split('-')[1],
						id:i
					}
				})
			}
		})
		// let days = meet.MEET_DAYS_SET;

		let dayNow1 = timeHelper.time('Y-M-D');
		let dayNow2 = timeHelper.time('Y-M-D', 86400);
		let dayNow3 = timeHelper.time('Y-M-D', 86400 * 2);

	
		for (let k = 0; k < days.length; k++) {
			if (days[k].day == dayNow1) days[k].status = '今天';
			if (days[k].day == dayNow2) days[k].status = '明天';
			if (days[k].day == dayNow3) days[k].status = '后天';
			days[k].week = timeHelper.week(days[k].day);
			days[k].date = days[k].day.split('-')[1] + '-' + days[k].day.split('-')[2]
		}
		this.setData({
			days: days,
			dateTime: dateTime
		})


		this._loadDetail();
	},

	_loadDetail: async function () {
		this.setData({
			dayIdx: 0,
			timeIdx: -1,
		});
	},

	bindDayTap: function (e) {
		let dayIdx = pageHelper.dataset(e, 'idx');
		this.setData({
			dayIdx,
			timeIdx: -1,
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	// onPullDownRefresh: async function () {
	// 	await this._loadDetail();
	// 	wx.stopPullDownRefresh();
	// },

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},

	bindTimeTap: function (e) {
		let timeIdx = pageHelper.dataset(e, 'timeidx');

		let node = this.data.days[this.data.dayIdx].times[timeIdx];
		if (node.error) return;

		this.setData({
			timeIdx
		});
	},

	bindJoinTap: async function (e) {
		let dayIdx = this.data.dayIdx;
		let timeIdx = this.data.timeIdx;
		if (timeIdx < 0) return pageHelper.showNoneToast('请先选择预约时段');

		const day = this.data.days[dayIdx].day
		const time = this.data.dateTime[timeIdx]
		
		wx.navigateTo({
			url: `../join/meet_join?day=${day}&time=${time}`,
		})
	},

	getFutureDate: function (breakDate) {
		let arr = new Array(breakDate);
		for (let i = 0; i < arr.length; i++) {
			let thisTime = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * i);
			let fullYear = thisTime.getFullYear();
			let month = (thisTime.getMonth() + 1);
			let date = thisTime.getDate();

			month = month < 10 ? '0' + month : month
			date = date < 10 ? '0' + date : date
			arr[i] = `${fullYear}-${month}-${date}`
		}
		return arr
	}

})